package com.chase.passwordmaster.mine.setting.pass

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.chase.passwordmaster.R
import com.chase.passwordmaster.data.GlobalData
import com.chase.passwordmaster.encoder.EncoderRefreshInfo
import com.chase.passwordmaster.encoder.refreshEncoder
import com.chase.passwordmaster.pass.encryptAndSaveLoginPassword
import com.chase.passwordmaster.utils.BCrypt
import com.chase.passwordmaster.utils.showToast
import com.chase.passwordmaster.utils.useDarkStatusBar
import com.chase.passwordmaster.utils.useImmersingNavigator

class SettingPassActivity : AppCompatActivity() {

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, SettingPassActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var cpBack: View
    private lateinit var cpInputNewPass: EditText
    private lateinit var cpInputOldPass: EditText
    private lateinit var cpSave: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_pass)

        cpBack = findViewById(R.id.back_setting_pass)
        cpInputOldPass = findViewById(R.id.input_old_pass_set_pass)
        cpInputNewPass = findViewById(R.id.input_new_pass_set_pass)
        cpSave = findViewById(R.id.save_pass_set_pass)

        useDarkStatusBar()
        useImmersingNavigator()

        initCPBack(cpBack = cpBack)
        initCPSave()
    }

    private fun initCPBack(cpBack: View) {
        cpBack.setOnClickListener { onBackPressed() }
    }

    private fun initCPSave() {
        cpSave.setOnClickListener {
            val oldPass = cpInputOldPass.text.toString()
            val newPass = cpInputNewPass.text.toString()

            if (!checkLoginPassword(oldPass)) {
                showToast("旧密码错误")
            } else {
                if (newPass.isNotBlank()) {
                    if (!encryptAndSaveLoginPassword(newPass)) {
                        showToast("新密码保存失败", Toast.LENGTH_LONG)
                    } else {
                        // Success
                        // Refresh rsa key pair by new login pass
                        GlobalData.loginPassword?.let { refreshEncoder(EncoderRefreshInfo(oldLoginPass = oldPass, newLoginPass = it)) }
                            ?: showToast("未知错误：密码保存成功但无法读取")
                        this.finish()
                    }
                } else {
                    showToast("密码不能为空")
                }
            }
        }
    }
}

/**
 * Check pass is correct or not
 * WARNING: encryptedLoginPass may be null!
 * @param pass input pass
 * @param encryptedLoginPass encrypted true login pass, default: pass from GlobalData
 * @return right?
 */
fun checkLoginPassword(
    pass: String,
    encryptedLoginPass: String? = GlobalData.encryptedLoginPasswordObject?.encodedLoginPassword
) = BCrypt.checkpw(pass, encryptedLoginPass)
